﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

public partial class Controls_BackEnd_UserEdit : UserControl
{

    protected Users User
    {
        get
        {
            var users = (Users)Session[SessionManager.SessionData] ?? new Users();
            users.UserName = txtUserName.Text;
            users.Email = txtEmail.Text;
            users.FullName = txtFullName.Text;
            users.Password = users.Password != txtPassword.Text.Trim()
                                 ? Config.Md5(txtPassword.Text.Trim())
                                 : users.Password;
            users.DepartmentID = int.Parse(ddlDepartment.SelectedValue);
            users.GroupID = int.Parse(ddlGroup.SelectedValue);
            users.IsActive = cbActive.Checked;
            return users;
        }
        set
        {
            Session[SessionManager.SessionData] = value;
            txtUserName.Text = value.UserName;
            txtPassword.Attributes.Add("value", value.Password);
            txtEmail.Text = value.Email;
            txtFullName.Text = value.FullName;
            if (value.DepartmentID > 0)
                ddlDepartment.SelectedValue = value.DepartmentID.ToString();
            if (value.GroupID > 0)
                ddlGroup.SelectedValue = value.GroupID.ToString();

        }
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        BinderDropDown();
        GetData();
    }



    private void BinderDropDown()
    {
        var listGroup = new Groups().Groups_LoadByCondition(string.Empty);
        foreach (var groupse in listGroup)
            ddlGroup.Items.Add(new ListItem { Value = groupse.GroupID.ToString(), Text = groupse.Name });
        var listDepartment = new Departments().Departments_GetByCondition(string.Empty);
        foreach (var departmentse in listDepartment)
            ddlDepartment.Items.Add(new ListItem { Value = departmentse.DepartmentID.ToString(), Text = departmentse.Name });
    }

    /// <summary>
    /// 
    /// </summary>
    private void GetData()
    {
        User = Config.Id > 0 ? new Users { UserID = Config.Id }.Users_GetByUserID() : new Users();
        CheckPermission();

    }



    /// <summary>
    /// 
    /// </summary>
    private void CheckPermission()
    {

        var permission = (UserFunctions)Session[SessionManager.SessionPermission];
        btnSave.Visible = Config.Id > 0 ? permission.IsUpdate : permission.IsInsert;
        btnReset.Visible = permission.IsInsert;
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AllCommand(object sender, EventArgs e)
    {
        switch (((Control)sender).ID)
        {
            case "btnReset":
                User = new Users();
                Config.Id = 0;
                break;
            case "btnBack":
                Config.Back();
                break;
            case "btnSave":
                var error = User.Save();
                if (error > 0)
                {
                    User.UserID = Config.Id = error;
                    Config.Message(Page, Resources.Resource.SaveSuccess, Resources.Resource.Messenger);
                    return;
                }
                switch (error)
                {
                    case -1:
                        Config.Message(Page, string.Format(Resources.Resource.ObjectExisted, Resources.Resource.User), Resources.Resource.Warning);
                        break;
                    case 0:
                        Config.Message(Page, string.Format(Resources.Resource.ObjectExisted, Resources.Resource.Email), Resources.Resource.Warning);
                        break;
                    default:
                        Config.Message(Page, Resources.Resource.ErrorMsg, Resources.Resource.Warning);
                        break;
                }
                break;

        }

    }
}
